layui.use(['layer', 'form', 'admin', 'ax', 'laytpl'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var admin = layui.admin;
    var laytpl = layui.laytpl;

    //获取所有角色码  并渲染
    var ajax = new $ax(system.contextPath + "/api/role.listall");
    ajax.setType("get");
    var result = ajax.start();
    if (result.status) {
        var dataArr = result.data;
        var data0 = {
            list: dataArr,
        };
        var part0Temp = document.getElementById('part0-template');
        laytpl(part0Temp.innerHTML).render(data0, function (html) {
            document.getElementById("part0").innerHTML = html;
            form.render();
        });
    }

    var roleCode = [];
    form.on('checkbox(roleCode)', function (data) {
        if (data.elem.checked) {
            roleCode.push(data.value);
        } else {
            var index = roleCode.indexOf(data.value);
            roleCode.splice(index, 1)
        }
    });

    var status = 1;
    form.on('switch(status)', function (data) {
        if (data.elem.checked) {
            status = 1;
        } else {
            status = 2;
        }
    });
    // 表单提交事件
    form.on('submit(btnSubmit)', function (data) {
        var ajax = new $ax(system.contextPath + "/api/isv.info.add", function (res) {
            if (res.status) {
                system.success("添加成功！");
                //传给上个页面，刷新table用
                admin.putTempData('formOk', true);
                //关掉对话框
                admin.closeThisDialog();
            } else {
                system.error("添加失败！" + res.msg);
            }
        }, function (res) {
            system.error("添加失败！" + res.msg);
        });
        data.field.roleCode = roleCode.join(",");
        data.field.status = status;
        ajax.set(data.field);
        ajax.start();
        return false;
    });
});